//
//  MoviesViewController.m
//  DouBan
//
//  Created by lanou3g on 15/8/8.
//  Copyright (c) 2015年 xuzheng. All rights reserved.
//

#import "MoviesViewController.h"
#import "DataTool.h"
#import "MoviesTableViewCell.h"
#import "DetailViewController.h"
#import "Movies.h"
#import "RootView.h"
@interface MoviesViewController ()

@end

@implementation MoviesViewController
- (void)loadView{
    self.rv=[[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view=self.rv;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //协议
    self.rv.table.dataSource=self;
    self.rv.table.delegate=self;
    //解析
    
    NSString  *SURL=@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    [DataTool solveDataWithURL:SURL andMethod:@"GET" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       NSLog(@"%@",dic);
        self.data=[NSMutableArray array];
        NSArray *arr = dic[@"events"];
        for (NSDictionary *dict in arr) {
            Movies *mov = [[Movies alloc]init];
            [mov setValuesForKeysWithDictionary:dict];
            [self.data addObject:mov];
        }
        for (Movies *m in self.data) {
            NSLog(@"%@---%@", m.pic_url,m.movieName);
        }
        
        [self.rv.table reloadData];
    }];
    
    //顶部实现
    //self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"电影";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barItem.png"] forBarMetrics:UIBarMetricsDefault];

    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"83-calendar.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem = right;
}

//顶部右侧按钮事件
- (void)rightAction{
    //切换事件
    
}
//设置一共有几个区域
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//某一区域cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MoviesTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell_id"];
    if (cell==nil) {
        cell=[[MoviesTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell_id"];
    }
    Movies *mo=self.data[indexPath.row];
    if ((mo.myImage ==nil)&&(mo.isLoading==NO)) {
        //给一个默认的图片
        cell.photoImage.image=[UIImage imageNamed:@"1.png"];
        [mo addObserver:self forKeyPath:@"myImage" options:NSKeyValueObservingOptionNew context:(__bridge void *)(indexPath)];
        [mo loadImage];
    }else{
        if (mo.myImage==nil) {
            cell.photoImage.image=[UIImage imageNamed:@"1.png"];
        }else{
            cell.photoImage.image=mo.myImage;
            cell.nameLabel.text = mo.movieName;
            cell.movIDLabel.text=mo.movieId;
        }
    }
    
    return cell;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    NSIndexPath *indexPath=(__bridge NSIndexPath *)context;//将contex转换为indexpath类型
    UIImage *tempImage=change[NSKeyValueChangeNewKey];
    if ([tempImage isEqual:[NSNull null]]) {
        return;
    }
    NSArray *arr=[self.rv.table indexPathsForVisibleRows];
    MoviesTableViewCell *cell=(MoviesTableViewCell *)[self.rv.table cellForRowAtIndexPath:indexPath];
    if ([arr containsObject:indexPath]) {
        cell.photoImage.image=tempImage;
        [self.rv.table reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}


//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
//点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *dvc=[[DetailViewController alloc]init];
    [self.navigationController pushViewController:dvc animated:YES];
}


//当视图出现时调用
- (void)viewDidAppear:(BOOL)animated{
    [self.rv.table reloadData];
}
























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
